package com.starleague.jpa.appengine;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.datanucleus.api.jpa.annotations.Extension;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.starleague.common.AbstractHasInfoProperties;
import com.starleague.jpa.JpaDao;

@MappedSuperclass
public abstract class GaeDao
        extends AbstractHasInfoProperties
        implements JpaDao<Key>
{
    public static final class SQL
    {
        public static final String FIELD_ID_KEY = "idKey";
        public static final String FIELD_ANCESTOR_KEY = "ancestorKey";
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    // @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private Key idKey;

    @Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
    private Key ancestorKey;

    // TODO: Modify tests to handle this
    @Version
    private int version;

    @Override
    public Key getId()
    {
        return this.idKey;
    }

    @Override
    public String getUid()
    {
        return (this.idKey != null) ? KeyFactory.keyToString(this.idKey) : null;
    }

    // public abstract void postLoad(final EntityManager entityManager) throws
    // StarLeagueException;
    // public abstract void preRemove(final EntityManager entityManager) throws
    // StarLeagueException;
    // public abstract void prePersist(final EntityManager entityManager) throws
    // StarLeagueException;
    // public abstract void preUpdate(final EntityManager entityManager) throws
    // StarLeagueException;

    @Override
    public Map<String, Object> infoProperties()
    {
        Map<String, Object> infoPropertyMap = new LinkedHashMap<>();
        // infoPropertyMap.put("id", this.id);
        infoPropertyMap.put("ancestorKey", this.ancestorKey);
        infoPropertyMap.put("uid", this.getUid());
        infoPropertyMap.put("version", this.version);
        return infoPropertyMap;
    }

    @Override
    public String infoPropertyClassName()
    {
        return getClass().getSimpleName() + " [" + this.idKey + "]";
    }

    @Override
    public String toString()
    {
        return toString(0);
    }
}

// @NamedQueries({
// // @NamedQuery(
// // name=TeamDAO.SQL.LOOKUP_BY_NAME,
// // query="SELECT t FROM com.starleague.persist.TeamDAO t WHERE t.name = :" +
// TeamDAO.SQL.FIELD_NAME),
// // @NamedQuery(
// // name=TeamDAO.SQL.LOOKUP_BY_ID,
// // query="SELECT t FROM com.starleague.persist.TeamDAO t WHERE t.id = :" +
// TeamDAO.SQL.FIELD_ID),
// // @NamedQuery(
// // name=TeamDao.SQL.LOOKUP_ALL,
// // query="SELECT t FROM TeamDao t"),
// })
// public static Collection<Team> lookupAll(
// final EntityManager entityManager)// throws Throwable
// {
// final List<Team> teamList = new ArrayList<Team>();
//
// try
// {
// final Query query = entityManager.createNamedQuery(TeamDao.SQL.LOOKUP_ALL);
//
// for (final Object obj : query.getResultList())
// {
// final TeamDao teamDao = (TeamDao)obj;
// teamDao.entityManager = entityManager;
// teamList.add(teamDao);
// }
//
// return teamList;
// }
// catch(final NoResultException e)
// {
// return teamList;
// }
// }

// private static class SQL
// {
// //private static final String LOOKUP_BY_NAME = "TeamDAO.lookupByName";
// //private static final String LOOKUP_BY_ID = "TeamDAO.lookupByID";
// private static final String LOOKUP_ALL = "TeamDao.lookupAll";
// //private static final String FIELD_NAME = "name";
// //private static final String FIELD_ID = "id";
// }
